import {Component, OnDestroy, OnInit} from '@angular/core';
import {EventMessage, EventService, EventSource} from '../../services/event.service';

import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser';
import {BookedWrapper} from 'src/app/entity/TT';
import {RequestReplyService} from 'src/app/services/request-reply.service';
import {PainterService} from 'src/app/services/painter.service';


@Component({
  selector: 'app-painter',
  templateUrl: './painter.component.html',
  styleUrls: ['./painter.component.scss']
})
export class PainterComponent implements OnInit, OnDestroy {

  iframe: SafeResourceUrl;
  showPainter = true;
  cursor = 'default';

  constructor(
    private painterService: PainterService,
    private eventService: EventService,
    private requestService: RequestReplyService,
    private sanitizer: DomSanitizer) {
  }

  ngOnInit() {
    console.log('PainterComponent ngOnInit...');
    this.init();
    this.painterService.init();
  }

  ngOnDestroy() {
    console.log('ngOnDestroy');
  }

  check() {

  }

  init() {
    this.eventService.subscribeEvent((evt: EventMessage) => {
      switch (evt.source) {
        case EventSource.TOGGLE_TOOLBOX_CHANGED:
          this.showPainter = evt.data;
          break;

        case EventSource.BOOKED:
          const bookedWrapper: BookedWrapper = evt.data;
          if (bookedWrapper.current) {
            const src = `${this.requestService.showMaterialUrl}?sn=${bookedWrapper.current.sn}`;
            this.iframe = this.sanitizer.bypassSecurityTrustResourceUrl(src);
          }
          break;

        case EventSource.CURSOR:{
          this.cursor = evt.data;
          break;
        }
      }
    });
  }


}
